package com.singzi.leetcode.N2100;

import java.util.*;
import java.util.concurrent.TransferQueue;

public class T2034 {
    public static void main(String[] args) {
        StockPrice stockPrice = new StockPrice();
    }
}
class StockPrice {
    HashMap<Integer, Integer> stockMap;//(事件戳， 价格）
    TreeMap<Integer, Integer> prices;//(价格， 此价格的数量）
    private int earlyTime;
    private int currentPrice;
    public StockPrice() {
         this.stockMap = new HashMap<>();
         this.prices  = new TreeMap<>();
         earlyTime = Integer.MIN_VALUE;
         currentPrice = 0;
    }

    public void update(int timestamp, int price) {
        int prevPrice  = stockMap.getOrDefault(timestamp, 0);
        stockMap.put(timestamp, price);
        if(prevPrice > 0){
            prices.put(prevPrice, prices.get(prevPrice)-1);
            if(prices.get(prevPrice) == 0) prices.remove(prevPrice);
        }
        prices.put(price, prices.getOrDefault(price, 0)+1);
        if(timestamp >= earlyTime){
            earlyTime = timestamp;
            currentPrice = price;
        }
    }

    public int current() {
        return currentPrice;

    }

    public int maximum() {

        return prices.lastKey();

    }

    public int minimum() {
        return prices.firstKey();
    }
}